<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {
            //给更换封面按钮绑定文件上传控件的点击事件
            $("#changeBtn").click(function () {
                $("#file").click();
            });
            //当此控件状态改变时提交表单去图片处理的资源去
            $("#file").change(function () {
                if($(this).val()){
                    $("#fileForm").ajaxSubmit(function (data) {
                        if(data.status==1){
                            //操作成功,给框子赋值
                            $("#coverUrl").attr("src",data.url);
                        }
                    });
                }
            });
            //获取所有的旅游地点
            $.get("/regions",function (data) {
                //拼进select里面
                $.each(data,function (index,ele) {
                    $('<option value="'+ele.id+'">'+ele.name+'</option>').appendTo("#regions");
                });
                //在我的游记页面,点击每个图片的连接会发一个请求到这个页面并带上每个文章的id
                // 用工具类获取url上参数id的值
                var params=getParams();
                if(params.id){
                    //设置编辑游记的表单地址
                    $("#detailForm").attr('action',"/travels/"+params.id);
                    //设置表单提交为put
                    $("input[name='_method']").val("put");
                    //查询该游记相关的信息
                    $.get("/travels/"+params.id,function (data) {
                        $(".travels").renderValues(data);
                    });

                }
            });
            //表单提交
            //把表单从同步变成异步
            //当提交表单只需要一个回调函数的时候,我们用的是以前的方法,现在我们还需要一个参数,指定提交表单前的操作
            $("#detailForm").ajaxForm({
                success:function (data) {
                    if(data.success){
                        //操作成功,跳转到我的游记页面
                        window.location.href="/mine/travels.html";
                    }
                },
                //表单提交前执行的函数
                beforeSubmit:function (arr) { //arr是提交表单时传递的参数
                    //除了表单里现有的,还要补充几个不在表单里的
                    arr.push({name:"coverUrl",value:$("#coverUrl").attr("src")});//里面是json
                    arr.push({name:"title",value:$("#title").val()});
                    arr.push({name:"state",value:state});
                    arr.push({name:"travelContent.content",value:Edr.getContent()});//使用插件的方法获取游记内容
                }
            });
            //定义变量表示当前文章的状态,保存和发布各自修改
            var state=0;//默认为0,草稿状态
            //保存按钮
            $("#saveBtn").click(function () {
                $("#detailForm").submit();
            });
            //发布按钮
            $("#releaseBtn").click(function () {
                state=1;//待发布状态,等待审核
                $("#detailForm").submit();
            });

        });
    </script>



</head>

<body>
<!--将文件上传控件包在表单里,二进制上传-->
<form id="fileForm" action="/images" method="post" enctype="multipart/form-data">
    <!--隐藏的文件上传控件-->
    <input id="file" type="file" style="display: none" name="file">
</form>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="saveBtn"><span>保存</span></i>
            <i class="fa fa-check" id="releaseBtn"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>
       
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
       

    </div>
    <div class="title">
        <input class="form-control" id="title" render-value="title" placeholder="请输入标题"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post" action="/travels">
            <input type="hidden" name="id" render-value="id"/>
            <input type="hidden" name="_method"/>
            <label>出行时间:</label>
            <input type="text" name="travelTime"  class="form-control" onclick="WdatePicker()" render-value="travelTime">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="regions" name="place.id"  render-value="place.id">

            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox"  name="isPublic" render-attr="checked=isPublic"
                          >是否公开
                </label>
            </div>

        </form>
    </div>

    <hr/>

</div>

<!-- 插件核心 -->
<script src="/js/plugins/eleditor/Eleditor.min.js"></script>
<!-- 如果需要图片上传 -->
<script src="/js/plugins/eleditor/webuploader.min.js"></script>

<script>
    //创建编辑器的实例对象
    var Edr = new Eleditor({
        el: '#contentEditor',//页面渲染的位置
        upload:{
            server: '/images',//上传图片的接口
        },
    });
</script>
</body>

</html>